/******************************************************************************************************
* Collection of subroutines for the control of the ADwin measurement data acquisition
*
* Created : Based on ADwin.c Version 1.11 by M. Sanna and started developted 22.08.1996 from Martin Hotze
*
*******************************************************************
* Version 2.00beta  P.H und M.S. 08.03.2015 First Version with multithreading support and errorcode return variable.
*					New function:
*                   - Data_type
*                   This Version supports new T12 functions
*					- e_Get_ADBFPar_All_Double
*                   - e_Get_ADBFPar_Double
*                   - e_Set_ADBFPar_Double
*
*
* Version 2.00    M.S. 03.07.2015
* Version 2.01    M.S. 28.01.2016 Use the same data type "short" for variable "DataNo".
*                                 Changed functions: Data_Length, GetData_String, String_Length, SetData_String
*                                 Bugfix: Get_Known_DeviceNo, Errorhandling corrected
*
* Function of the DLL ADwin32(32-bit, respectively)
* =====================================================================
* Attention! the file "ADWIN32.DLL" or "ADWIN64.DLL" respectively have to be included in a
* subdirectory that can be accessed by Windows ( at best in " \windows" or "\winnt")
* in order to use these functions the program "ADwin12.btl" (or ADwin4.btl or ADwin5.btl or ADwin8.btl
* or ADwin9.btl or ADwin10.btl or ADwin11.btl
* respectively) has to be loaded to
* the ADwin system!
* The DLL ADwin32.dll or ADwin64.dll respectively include all functions necessary for
* the data transfer between PC and the ADwin system.
*
* To Change from ADwin.c ->ADwin.cpp use #include "stdafx.h"
*******************************************************************************************************/

#include "stdafx.h"	  // necessary for MFC projects
#include "windows.h"
#include "Adwin_V2.h"
#include <stdio.h>

int	FirstTime  = 1;
char *Type_Array[] = {"Undefined", "Byte", "Short", "Int32", "Long", "Float32", "Float64", "Int64", "String"};


static HINSTANCE DLLHandle;

long  (FAR PASCAL *Clear_Process_Ptr)(short ProcessNo, short Device_No,long *ErrorNo);
long  (FAR PASCAL *Boot_Ptr)(char *Filename, short Device_No, long Memsize, short msgbox, long *ErrorNo );
short (FAR PASCAL *Load_Process_Ptr)(char *Filename, short Device_No, short msgbox,long *ErrorNo);
short (FAR PASCAL *Start_Process_Ptr)(short Index, short Device_No, long *ErrorNo);
short (FAR PASCAL *Stop_Process_Ptr)(short Index, short Device_No, long *ErrorNo);
short (FAR PASCAL *Set_Par_Ptr)(short Index, long Value, short Device_No, long *ErrorNo);
short (FAR PASCAL *Set_FPar_Ptr)(short Index, float Value, short Device_No, long *ErrorNo);
long  (FAR PASCAL *Get_Par_Ptr)(short Index, short Device_No, long *Error_No);
float (FAR PASCAL *Get_FPar_Ptr)(short Index, short Device_No, long *ErrorNo);

short (FAR PASCAL *Set_FPar_Double_Ptr)(short Index, double Value, short Device_No, long * ErrorNo );
double (FAR PASCAL *Get_FPar_Double_Ptr)(short Index, short Device_No, long *ErrorNo);

short (FAR PASCAL *Get_Data_Ptr)(void *Data, short typ, short DataNo, long Startindex, long Count , short Device_No, long *ErrorNo);
short (FAR PASCAL *Set_Data_Ptr)(void *Data, short typ, short DataNo, long Startindex, long Count , short Device_No, long *ErrorNo);
short (FAR PASCAL *Save_Fast_Ptr)(char *Filename, short DataNo, long Startindex, long Count, short Mode, short Device_No,long *ErrorNo);
short (FAR PASCAL *File2Data_Ptr)(char *Filename, short typ, short DataNo, long Startindex, short Device_No);
short (FAR PASCAL *Get_Fifo_Ptr)(void *Data, short typ, short FifoNo, long Count , short Device_No,long *ErrorNo);
short (FAR PASCAL *Set_Fifo_Ptr)(void *Data, short typ, short FifoNo, long Count , short Device_No,long *ErrorNo);
long  (FAR PASCAL *Get_Fifo_Count_Ptr)(short FifoNo, short Device_No,long *ErrorNo);
long  (FAR PASCAL *Get_Fifo_Empty_Ptr)(short FifoNo, short Device_No,long *ErrorNo);
short (FAR PASCAL *Fifo_Clear_Ptr)(short FifoNo, short Device_No, long *ErrorNo);

short (FAR PASCAL *Workload_Ptr)(long Priority, short Device_No, long *ErrorNo);

short (FAR PASCAL *ADTest_Version_Ptr)(short Device_No, short msgbox, long *ErrorNo);
long  (FAR PASCAL *AD_Memory_Ptr)(short Device_No, long *ErrorNo);
long  (FAR PASCAL *AD_Memory_all_Ptr)(short Mem_Spec, short Device_No, long *ErrorNo);
long  (FAR PASCAL *AD_Memory_all_byte_Ptr)(short Mem_Spec, short Device_No, long *ErrorNo);
short (FAR PASCAL *ADProzessorTyp_Ptr)(short DeviceNo, long *ErrorNo);

// evtl. entfernen
long  (FAR PASCAL *AD_GetErrorCode_Ptr)(short DeviceNo);		// entl. entfernen

char  (FAR PASCAL *AD_GetErrorText_Ptr)(long ErrorCode, char *text, long lenght);
short (FAR PASCAL *Get_ADBPar_All_Ptr)(short Startindex, short Count, long *Array, short Device_No, long *ErrorNo);
short (FAR PASCAL *Get_ADBFPar_All_Ptr)(short Startindex, short Count, float *Array, short Device_No, long *ErrorNo);
short (FAR PASCAL *Get_ADBFPar_All_Double_Ptr)(short Startindex, short Count, double *Array, short Device_No, long *ErrorNo);
long  (FAR PASCAL *Get_Data_Length_Ptr)(short DataNo, short Device_No, long *ErrorNo);
short (FAR PASCAL *Get_Data_Type_Ptr)(short DataNo, short Device_No, long *ErrorNo);
long  (FAR PASCAL *ADSetLanguage_Ptr)(long language);
long  (FAR PASCAL *GetData_String_Ptr)(char *Data, long MaxCount, short DataNo, short Device_No, long *ErrorNo);
long  (FAR PASCAL *String_Length_Ptr)(short DataNo, short Device_No, long *ErrorNo);
long  (FAR PASCAL *SetData_String_Ptr)(char *Data, short DataNo, short Device_No, long *ErrorNo);
short (FAR PASCAL *GetData_Packed_Ptr)(void *Data, short Typ, short DataNo, long Startindex, long Count, short Device_No, long *ErrorNo);

short (FAR PASCAL *Get_Known_DeviceNo_Ptr)(short *Devices, long *Count_Devices);
long  (FAR PASCAL *Get_Known_USB_SerialNo_Ptr)(long *SerialNo);
int   (FAR PASCAL *ADwin_Debug_Mode_On_Ptr)(char *Filename, long Size);
int   (FAR PASCAL *ADwin_Debug_Mode_Off_Ptr)(void);
//05.05.04 MS
long  (FAR PASCAL *Get_Dev_ID_Ptr)(short Device_No, long *value, long *ErrorNo);
long  (FAR PASCAL *Get_Connection_Type_Ptr)(short Device_No);

							/* Error message from the DLL On(1)/Off(0) */
short message=1;            /* Error message if funktion fails */
							/* Iserv, ADBPrLoad and Test.	0 = don't show error message */
							/*								1 = show error message */

/*************************************************/
/* Load DLL and get the address of the function  */
/*************************************************/
int LoadDLLIfNeeded(void)
{

long	line = 0 ;
char    msg[84] ;

	//If adwin32.dll handle exist, don't load pointers again
if (DLLHandle)
	{
    return 0;
	}
	//Depending on the compiler setting x86/x64 the correct adwin32.dll or adwin64.dll will be loaded.

#ifdef _WIN64
	#ifdef UNICODE
	DLLHandle =  LoadLibrary(L"adwin64.dll");
	#else
	DLLHandle =  LoadLibrary("adwin64.dll");
	#endif // !UNICODE
#else
	#ifdef UNICODE
	DLLHandle =  LoadLibrary(L"adwin32.dll");
	#else
	DLLHandle =  LoadLibrary("adwin32.dll");
	#endif // !UNICODE
#endif

if (DLLHandle == NULL)
	{
    return ( k_failed_to_load_DLL_error );
    }

    if ((File2Data_Ptr = (short (FAR PASCAL *)(char *, short, short, long, short ))GetProcAddress(DLLHandle,"File2Data"))==0)
		{
		line = __LINE__ ;
		goto FunctionNotFoundError;
		}

    if ((Get_Data_Length_Ptr = (long (FAR PASCAL *)(short, short, long *))GetProcAddress(DLLHandle,"e_GetDataLength"))==0)
		{
		line = __LINE__ ;
		goto FunctionNotFoundError;
		}

	if ((Get_Data_Type_Ptr = (short (FAR PASCAL *)(short, short, long *))GetProcAddress(DLLHandle,"e_GetDataTyp"))==0)
		{
		line = __LINE__ ;
		goto FunctionNotFoundError;
		}

    if ((Boot_Ptr = (long (FAR PASCAL *)(char *, short, long, short, long *))GetProcAddress(DLLHandle,"e_ADboot"))==0)
		{
		line = __LINE__ ;
		goto FunctionNotFoundError;
		}

	if ((Get_ADBFPar_All_Double_Ptr = (short (FAR PASCAL *)(short, short, double *, short , long *))GetProcAddress(DLLHandle,"e_Get_ADBFPar_All_Double"))==0)
		{
		line = __LINE__ ;
		goto FunctionNotFoundError;
		}

    if ((Get_ADBFPar_All_Ptr = (short (FAR PASCAL *)(short, short, float *, short , long *))GetProcAddress(DLLHandle,"e_Get_ADBFPar_All"))==0)
		{
		line = __LINE__ ;
		goto FunctionNotFoundError;
		}

	if ((Get_ADBPar_All_Ptr = (short (FAR PASCAL *)(short, short, long *, short, long *ErrorNo))GetProcAddress(DLLHandle,"e_Get_ADBPar_All"))==0)
		{
		line = __LINE__ ;
		goto FunctionNotFoundError;
		}

	if ((Get_FPar_Double_Ptr = (double (FAR PASCAL *)(short, short,long *))GetProcAddress(DLLHandle,"e_Get_ADBFPar_Double"))==0)
		{
		line = __LINE__ ;
		goto FunctionNotFoundError;
		}

    if ((Get_FPar_Ptr = (float (FAR PASCAL *)(short, short,long *))GetProcAddress(DLLHandle,"e_Get_ADBFPar"))==0)
		{
		line = __LINE__ ;
		goto FunctionNotFoundError;
		}

    if ((Get_Par_Ptr = (long (FAR PASCAL *)(short, short,long *))GetProcAddress(DLLHandle,"e_Get_ADBPar"))==0)
		{
		line = __LINE__ ;
		goto FunctionNotFoundError;
		}

	if ((Set_FPar_Double_Ptr = (short (FAR PASCAL *)(short, double, short,long*))GetProcAddress(DLLHandle,"e_Set_ADBFPar_Double"))==0)
		{
		line = __LINE__ ;
		goto FunctionNotFoundError;
		}

    if ((Set_FPar_Ptr = (short (FAR PASCAL *)(short, float, short,long *))GetProcAddress(DLLHandle,"e_Set_ADBFPar"))==0)
		{
		line = __LINE__ ;
		goto FunctionNotFoundError;
		}

    if ((Set_Par_Ptr = (short (FAR PASCAL *)(short, long, short, long *))GetProcAddress(DLLHandle,"e_Set_ADBPar"))==0)
		{
		line = __LINE__ ;
		goto FunctionNotFoundError;
		}

    if ((Load_Process_Ptr = (short (FAR PASCAL *)(char *, short, short,long *))GetProcAddress(DLLHandle,"e_ADBload"))==0)
		{
		line = __LINE__ ;
		goto FunctionNotFoundError;
		}

    if ((Start_Process_Ptr = (short (FAR PASCAL *)(short, short, long *))GetProcAddress(DLLHandle,"e_ADB_Start"))==0)
		{
		line = __LINE__ ;
		goto FunctionNotFoundError;
		}

    if ((Stop_Process_Ptr = (short (FAR PASCAL *)(short, short, long *))GetProcAddress(DLLHandle,"e_ADB_Stop"))==0)
		{
		line = __LINE__ ;
		goto FunctionNotFoundError;
		}

    if ((Get_Data_Ptr = (short (FAR PASCAL *)(void*, short, short, long, long, short,long *))GetProcAddress(DLLHandle,"e_Get_Data"))==0)
		{
		line = __LINE__ ;
		goto FunctionNotFoundError;
		}

    if ((Set_Data_Ptr = (short (FAR PASCAL *)(void*, short, short, long, long, short, long *))GetProcAddress(DLLHandle,"e_Set_Data"))==0)
		{
		line = __LINE__ ;
		goto FunctionNotFoundError;
		}

    if ((Save_Fast_Ptr = (short (FAR PASCAL *)(char *, short, long, long, short, short, long *))GetProcAddress(DLLHandle,"e_SaveFast"))==0)
		{
		line = __LINE__ ;
		goto FunctionNotFoundError;
		}

	if ((Get_Fifo_Ptr = (short (FAR PASCAL *)(void*, short, short, long, short, long *))GetProcAddress(DLLHandle,"e_Get_Fifo"))==0)
		{
		line = __LINE__ ;
		goto FunctionNotFoundError;
		}

    if ((Set_Fifo_Ptr = (short (FAR PASCAL *)(void*, short, short, long, short, long *))GetProcAddress(DLLHandle,"e_Set_Fifo"))==0)
		{
		line = __LINE__ ;
		goto FunctionNotFoundError;
		}

    if ((Get_Fifo_Count_Ptr = (long (FAR PASCAL *)(short, short, long *))GetProcAddress(DLLHandle,"e_Get_Fifo_Count"))==0)
		{
		line = __LINE__ ;
		goto FunctionNotFoundError;
		}

    if ((Get_Fifo_Empty_Ptr = (long (FAR PASCAL *)(short, short, long *))GetProcAddress(DLLHandle,"e_Get_Fifo_Empty"))==0)
		{
		line = __LINE__ ;
		goto FunctionNotFoundError;
		}

    if ((Fifo_Clear_Ptr = (short (FAR PASCAL *)(short, short, long *))GetProcAddress(DLLHandle,"e_Clear_Fifo"))==0)
		{
		line = __LINE__ ;
		goto FunctionNotFoundError;
		}

    if ((Workload_Ptr = (short (FAR PASCAL *)(long, short, long *))GetProcAddress(DLLHandle,"e_AD_Workload"))==0)
		{
		line = __LINE__ ;
		goto FunctionNotFoundError;
		}

    if ((ADTest_Version_Ptr = (short (FAR PASCAL *)(short, short, long *))GetProcAddress(DLLHandle,"e_ADTest_Version"))==0)
		{
		line = __LINE__ ;
		goto FunctionNotFoundError;
		}

    if ((AD_Memory_Ptr = (long (FAR PASCAL *)(short, long *))GetProcAddress(DLLHandle,"e_AD_Memory"))==0)
		{
		line = __LINE__ ;
		goto FunctionNotFoundError;
		}

    if ((AD_Memory_all_Ptr = (long (FAR PASCAL *)(short, short, long *))GetProcAddress(DLLHandle,"e_AD_Memory_all"))==0)
		{
		line = __LINE__ ;
		goto FunctionNotFoundError;
		}

    if ((AD_GetErrorCode_Ptr = (long (FAR PASCAL *)(short))GetProcAddress(DLLHandle,"ADGetErrorCode"))==0)
		{
		line = __LINE__ ;
		goto FunctionNotFoundError;
		}

    if ((AD_GetErrorText_Ptr = (char (FAR PASCAL *)(long, char *, long))GetProcAddress(DLLHandle,"ADGetErrorText"))==0)
		{
		line = __LINE__ ;
		goto FunctionNotFoundError;
		}

    if ((ADSetLanguage_Ptr = (long (FAR PASCAL *)(long))GetProcAddress(DLLHandle,"ADSetLanguage"))==0)
		{
		line = __LINE__ ;
		goto FunctionNotFoundError;
		}

    if ((GetData_String_Ptr = (long (FAR PASCAL *)(char *, long, short, short, long *))GetProcAddress(DLLHandle,"e_Get_Data_String"))==0)
		{
		line = __LINE__ ;
		goto FunctionNotFoundError;
		}

    if ((String_Length_Ptr = (long (FAR PASCAL *)(short, short, long *))GetProcAddress(DLLHandle,"e_Get_Data_String_Length"))==0)
		{
		line = __LINE__ ;
		goto FunctionNotFoundError;
		}

    if ((SetData_String_Ptr = (long (FAR PASCAL *)(char *, short, short, long *))GetProcAddress(DLLHandle,"e_Set_Data_String"))==0)
		{
		line = __LINE__ ;
		goto FunctionNotFoundError;
		}

    if ((GetData_Packed_Ptr = (short (FAR PASCAL *)(void *, short, short, long, long, short, long *))GetProcAddress(DLLHandle,"e_Get_Data_packed"))==0)
		{
		line = __LINE__ ;
		goto FunctionNotFoundError;
		}

    if ((ADProzessorTyp_Ptr = (short (FAR PASCAL *)(short Device_No, long *))GetProcAddress(DLLHandle,"e_ProzessorTyp"))==0)
		{
		line = __LINE__ ;
		goto FunctionNotFoundError;
		}

    if ((Clear_Process_Ptr = (long (FAR PASCAL *)(short, short,long *))GetProcAddress(DLLHandle,"e_Clear_Process"))==0)
		{
		line = __LINE__ ;
		goto FunctionNotFoundError;
		}

    if ((Get_Known_DeviceNo_Ptr = (short (FAR PASCAL *)(short *, long *))GetProcAddress(DLLHandle,"Get_Known_Deviceno"))==0)
		{
		line = __LINE__ ;
		goto FunctionNotFoundError;
		}

    if ((Get_Known_USB_SerialNo_Ptr = (long (FAR PASCAL *)(long *))GetProcAddress(DLLHandle,"Get_Known_USB_SerialNo"))==0)
		{
		line = __LINE__ ;
		goto FunctionNotFoundError;
		}

    if ((ADwin_Debug_Mode_On_Ptr = (int (FAR PASCAL *)(char  *, long))GetProcAddress(DLLHandle,"adwin_debug_mode_on"))==0)
		{
		line = __LINE__ ;
		goto FunctionNotFoundError;
		}

    if ((ADwin_Debug_Mode_Off_Ptr = (int (FAR PASCAL *)(void))GetProcAddress(DLLHandle,"adwin_debug_mode_off"))==0)
		{
		line = __LINE__ ;
		goto FunctionNotFoundError;
		}

    if ((Get_Dev_ID_Ptr = (long (FAR PASCAL *)(short, long *, long *))GetProcAddress(DLLHandle,"e_get_dev_id"))==0)
		{
		line = __LINE__ ;
		goto FunctionNotFoundError;
		}

    if ((Get_Connection_Type_Ptr = (long (FAR PASCAL *)(short))GetProcAddress(DLLHandle,"ADGetConnectionTyp"))==0)
		{
		line = __LINE__ ;
		goto FunctionNotFoundError;
		}

    if ((AD_Memory_all_byte_Ptr = (long (FAR PASCAL *)(short, short, long *))GetProcAddress(DLLHandle,"e_AD_Memory_all_byte"))==0)
		{
		line = __LINE__ ;
		goto FunctionNotFoundError;
		}

return 0;


							/* Error handling */
FunctionNotFoundError:

if (FirstTime==1)
	{
#ifdef UNICODE
	MessageBox(NULL, L"You need a newer version of the ADwin32.dll or ADwin64.dll,\nat least ADwin32/64.dll from 18.9.2014!\nAbort the program and update the ADwin software now.", L"ADwin32/64.dll too old!", MB_ICONWARNING);
#else
	MessageBox(NULL, "You need a newer version of the ADwin32.dll or ADwin64.dll,\nat least ADwin32/64.dll from 18.9.2014!\nAbort the program and update the ADwin software now.", "ADwin32/64.dll too old!", MB_ICONWARNING);

	if( line != 0 )
	{
		//sprintf( &msg[0] , "Error in line %d." , line-2 ) ;
		sprintf_s( &msg[0] ,80 , "Error in line %d." , line-2 ) ;
		MessageBox(NULL , &msg[0] , "" , MB_ICONWARNING);
	}


#endif // !UNICODE


	FirstTime = 0;			/* No more messages */
	}

FreeLibrary(DLLHandle);     /* Remove the DLL */
DLLHandle = 0;

return( k_could_not_find_function ) ;
}

/*****************************************/
/* Connection code to the DLL functions  */
/*****************************************/

/* Down-loads the operating system (BTL-file) to the ADwin-System */
/* ============================================================================== */
void Boot(char *Filename, short DeviceNo, long *ErrorNo)
{
    long dllLoadError;
    if ((dllLoadError = (long)LoadDLLIfNeeded())!=0)
	{
		*ErrorNo = dllLoadError;
	}
	else
	{
		(*Boot_Ptr)(Filename, DeviceNo, 0, message, ErrorNo );							/* Boot the ADwin card */
	}
}


/* Loads an ADbasic-process (a bin file generated by ADbasic) to the ADwin board  */
/* ============================================================================== */
void Load_Process(char *Filename , short DeviceNo,long *ErrorNo)
{
    long dllLoadError;
    if ((dllLoadError = LoadDLLIfNeeded())!=0)
	{
        *ErrorNo = dllLoadError;
	}
	else
	{
	 	(*Load_Process_Ptr)(Filename, DeviceNo, message,ErrorNo);	/* Load bin file */
	}
}


/* Starts process number "ProcessNo" on the ADwin-System						  */
/* ============================================================================== */
void Start_Process(short ProcessNo , short DeviceNo,long *ErrorNo)
{
    long dllLoadError;
    if ((dllLoadError = LoadDLLIfNeeded())!=0)
	{
        *ErrorNo = dllLoadError;
	}
	else
	{
		(*Start_Process_Ptr)(ProcessNo, DeviceNo,ErrorNo);	/* Start process */
	}
}


/* Stops process number "ProcessNo" on the ADwin-System							  */
/* ============================================================================== */
void Stop_Process(short ProcessNo , short DeviceNo,long *ErrorNo)
{
    long dllLoadError;
    if ((dllLoadError = LoadDLLIfNeeded())!=0)
	{
        *ErrorNo = dllLoadError;
	}
	else
	{
		(*Stop_Process_Ptr)(ProcessNo, DeviceNo,ErrorNo);	/* Stop process */
	}
}


/* Sets a parameter (PAR_"Index") on the ADwin-System to "Value"				  */
/* ============================================================================== */
void Set_Par (short Index, long Value, short DeviceNo , long *ErrorNo)
{
    long dllLoadError;
    if ((dllLoadError = LoadDLLIfNeeded())!=0)
	{
        *ErrorNo = dllLoadError;
	}
	else
	{
		(*Set_Par_Ptr)(Index, Value, DeviceNo, ErrorNo);		/* Set parameter */
	}
}

/* Sets a parameter (FPAR_"Index") on the ADwin-System to "Value"				  */
/* ============================================================================== */
void Set_FPar (short Index, float Value, short DeviceNo , long *ErrorNo)
{
    long dllLoadError;
    if ((dllLoadError = LoadDLLIfNeeded())!=0)
	{
        *ErrorNo = dllLoadError;
	}
	else
	{
		(*Set_FPar_Ptr)(Index, Value, DeviceNo,ErrorNo);		/* Set float parameter */
	}
}


/* Returns double value of parameter (FPAR_"Index") from the ADwin-System          */
/* ============================================================================== */
void Set_FPar_Double(short Index, double Value, short DeviceNo , long *ErrorNo)
{
    long dllLoadError;
    if ((dllLoadError = LoadDLLIfNeeded())!=0)
	{
        *ErrorNo = dllLoadError;
	}
	else
	{
		(*Set_FPar_Double_Ptr)(Index, Value, DeviceNo,ErrorNo);            /* Set Double parameter */
	}
}


/* Returns the value of parameter (PAR_"Index") from the ADwin-System			  */
/* ============================================================================== */
long Get_Par(short Index , short DeviceNo , long *ErrorNo)
{
    long dllLoadError;
    if ((dllLoadError = LoadDLLIfNeeded())!=0)
	{
        *ErrorNo = dllLoadError;
		return -1;
	}
	else
	{
		return (*Get_Par_Ptr)(Index,DeviceNo,ErrorNo);				/* Get parameter */
	}
}


/* Returns float value of parameter (FPAR_"Index") from the ADwin-System          */
/* ============================================================================== */
float Get_FPar(short Index , short DeviceNo , long *ErrorNo)
{
    long dllLoadError;
    if ((dllLoadError = LoadDLLIfNeeded())!=0)
	{
        *ErrorNo = dllLoadError;
		return -1;
	}
	else
	{
		return (*Get_FPar_Ptr)(Index, DeviceNo, ErrorNo);            /* Get float parameter */
	}
}

/* Returns double value of parameter (FPAR_"Index") from the ADwin-System          */
/* ============================================================================== */
double Get_FPar_Double(short Index , short DeviceNo , long *ErrorNo)
{
    long dllLoadError;
    if ((dllLoadError = LoadDLLIfNeeded())!=0)
	{
        *ErrorNo = dllLoadError;
		return -1;
	}
	else
	{
		return (*Get_FPar_Double_Ptr)(Index, DeviceNo,ErrorNo);            /* Get float parameter */
	}
}
/* Gets all 80 ADwin double parameters (FPar_1 - FPar_80) into a double array					  */
/* If T9..T11: Gets all 80 ADwin float parameters (FPar_1 - FPar_80) into a double array					  */
/* ============================================================================== */
void Get_FPar_All_Double (double Array[] , short DeviceNo , long *ErrorNo)
{
    long dllLoadError;
    if ((dllLoadError = LoadDLLIfNeeded())!=0)
	{
        *ErrorNo = dllLoadError;
	}
	else
	{
		(*Get_ADBFPar_All_Double_Ptr)(1, 80, Array, DeviceNo,ErrorNo);            /* Get float parameter */
	}
}

/* Gets a block of ADwin double parameters into a double array					  */
/* ============================================================================== */
void Get_FPar_Block_Double(double Array[], short Startindex, short Count, short DeviceNo , long *ErrorNo)
{
    long dllLoadError;
    if ((dllLoadError = LoadDLLIfNeeded())!=0)
	{
        *ErrorNo = dllLoadError;
	}
	else
	{
	   (*Get_ADBFPar_All_Double_Ptr)(Startindex, Count, Array, DeviceNo, ErrorNo);
	}
}


/* Returns array-element(s) of long-type from ADwin-System-array (DATA_"DataNo")  */
/* ============================================================================== */
void GetData_Long (short DataNo, long Data[], long Startindex, long Count, short DeviceNo , long * ErrorNo)
{
    long dllLoadError;
    if ((dllLoadError = LoadDLLIfNeeded())!=0)
	{
        *ErrorNo = dllLoadError;
	}
	else
	{
		(*Get_Data_Ptr)(Data, TYPE_LONG, DataNo, Startindex, Count, DeviceNo,ErrorNo);
	}
}

/* Returns array-element(s) of double-type from ADwin-System-array (DATA_"DataNo") */
/* ==============================================================================  */
void GetData_Double (short DataNo, double Data[],long Startindex, long Count, short DeviceNo , long * ErrorNo)
{
    long dllLoadError;
    if ((dllLoadError = LoadDLLIfNeeded())!=0)
	{
        *ErrorNo = dllLoadError;
	}
	else
	{
		(*Get_Data_Ptr)(Data, TYPE_DOUBLE, DataNo, Startindex, Count, DeviceNo, ErrorNo);
	}
}


/* Returns array-element(s) of float-type from ADwin-System-array (DATA_"DataNo") */
/* ============================================================================== */
void GetData_Float (short DataNo, float Data[], long Startindex, long Count, short DeviceNo , long * ErrorNo)
{
    long dllLoadError;
    if ((dllLoadError = LoadDLLIfNeeded())!=0)
	{
        *ErrorNo = dllLoadError;
	}
	else
	{
		(*Get_Data_Ptr)(Data, TYPE_FLOAT, DataNo, Startindex, Count, DeviceNo, ErrorNo);
	}
}


/* Sets array-element(s) of ADwin-System from long-array						  */
/* ============================================================================== */
void SetData_Long(short DataNo, long Data[], long Startindex, long Count, short DeviceNo , long *ErrorNo)
{
    long dllLoadError;
    if ((dllLoadError = LoadDLLIfNeeded())!=0)
	{
        *ErrorNo = dllLoadError;
	}
	else
	{
		(*Set_Data_Ptr)(Data, TYPE_LONG, DataNo, Startindex, Count, DeviceNo,ErrorNo);
	}
}

/* Sets array-element(s) of ADwin-System from double-array						  */
/* ============================================================================== */
void SetData_Double(short DataNo, double Data[], long Startindex, long Count, short DeviceNo , long *ErrorNo)
{
    long dllLoadError;
    if ((dllLoadError = LoadDLLIfNeeded())!=0)
	{
        *ErrorNo = dllLoadError;
	}
	else
	{
		(*Set_Data_Ptr)(Data, TYPE_DOUBLE, DataNo, Startindex, Count, DeviceNo,ErrorNo);
	}
}

/* Sets array-element(s) of ADwin-System from float-array						   */
/* =============================================================================== */
void SetData_Float(short DataNo, float Data[], long Startindex, long Count, short DeviceNo , long *ErrorNo)
{
    long dllLoadError;
    if ((dllLoadError = LoadDLLIfNeeded())!=0)
	{
        *ErrorNo = dllLoadError;
	}
	else
	{
		(*Set_Data_Ptr)(Data, TYPE_FLOAT, DataNo, Startindex, Count, DeviceNo,ErrorNo);
	}
}

/* Writes array-elements of ADwin-System immediately to harddisk			    */
/* ============================================================================ */
void Data2File(char *Filename, short DataNo, long Startindex, long Count, short Mode, short DeviceNo , long *ErrorNo)
{
    long dllLoadError;
    if ((dllLoadError = LoadDLLIfNeeded())!=0)
	{
        *ErrorNo = dllLoadError;
	}
	else
	{
		(*Save_Fast_Ptr)(Filename, DataNo, Startindex, Count, Mode, DeviceNo,ErrorNo);
	}
}

/* Writes a file immediately from harddisk to array-elements of ADwin-System 			    */
/* ============================================================================ */
void File2Data(char *Filename, short DataType, short DataNo, long Startindex, short DeviceNo , long *ErrorNo)
{
    long dllLoadError;
    if ((dllLoadError = LoadDLLIfNeeded())!=0)
	{
        *ErrorNo = dllLoadError;
	}
	else
	{
		//Hint: DataType constant
		//			TYPE_LONG;   2= long
	    // 			TYPE_FLOAT;  5= float
	    //			TYPE_DOUBLE; 6= double
		*ErrorNo = (*File2Data_Ptr)(Filename, DataType, DataNo, Startindex, DeviceNo) ;
	}
}


/* Fetching the long-element(s) from a ADwin-System FIFO						  */
/* ============================================================================== */
void GetFifo_Long(short FifoNo, long Data[], long Count, short DeviceNo , long * ErrorNo)
{
    long dllLoadError;
    if ((dllLoadError = LoadDLLIfNeeded())!=0)
	{
        *ErrorNo = dllLoadError;
	}
	else
	{
		(*Get_Fifo_Ptr)(Data, TYPE_LONG, FifoNo, Count, DeviceNo, ErrorNo);
	}
}

/* Fetching the double-element(s) from a ADwin-System FIFO						  */
/* ============================================================================== */
void GetFifo_Double(short FifoNo, double Data[], long Count, short DeviceNo , long * ErrorNo)
{
    long dllLoadError;
    if ((dllLoadError = LoadDLLIfNeeded())!=0)
	{
        *ErrorNo = dllLoadError;
	}
	else
	{
		(*Get_Fifo_Ptr)(Data, TYPE_DOUBLE, FifoNo, Count, DeviceNo, ErrorNo);
	}
}


/* Fetching the float-element(s) from a ADwin-System FIFO						  */
/* ============================================================================== */
void GetFifo_Float(short FifoNo, float Data[], long Count, short DeviceNo , long * ErrorNo)
{
    long dllLoadError;
    if ((dllLoadError = LoadDLLIfNeeded())!=0)
	{
        *ErrorNo = dllLoadError;
	}
	else
	{
		(*Get_Fifo_Ptr)(Data, TYPE_FLOAT, FifoNo, Count, DeviceNo, ErrorNo);
	}
}


/* Sets FIFO-element(s) of ADwin-System from long-array							  */
/* ============================================================================== */
void SetFifo_Long(short FifoNo, long Data[], long Count, short DeviceNo , long *ErrorNo)
{
    long dllLoadError;
    if ((dllLoadError = LoadDLLIfNeeded())!=0)
	{
        *ErrorNo = dllLoadError;
	}
	else
	{
		(*Set_Fifo_Ptr)(Data, TYPE_LONG, FifoNo, Count, DeviceNo,ErrorNo);
	}
}

/* Sets FIFO-element(s) of ADwin-System from double-array						  */
/* ============================================================================== */
void SetFifo_Double(short FifoNo, double Data[], long Count, short DeviceNo , long *ErrorNo)
{
    long dllLoadError;
    if ((dllLoadError = LoadDLLIfNeeded())!=0)
	{
        *ErrorNo = dllLoadError;
	}
	else
	{
		(*Set_Fifo_Ptr)(Data, TYPE_DOUBLE, FifoNo, Count, DeviceNo,ErrorNo);
	}
}

/* Sets FIFO-element(s) of ADwin-System from float-array						  */
/* ============================================================================== */
void SetFifo_Float(short FifoNo, float Data[], long Count, short DeviceNo , long *ErrorNo)
{
    long dllLoadError;
    if ((dllLoadError = LoadDLLIfNeeded())!=0)
	{
        *ErrorNo = dllLoadError;
	}
	else
	{
		(*Set_Fifo_Ptr)(Data, TYPE_FLOAT, FifoNo, Count, DeviceNo,ErrorNo);
	}
}


/* Getting the number of elements in the FIFO									  */
/* ============================================================================== */
long Fifo_Full(short FifoNo, short DeviceNo , long *ErrorNo)
{
    long dllLoadError;
    if ((dllLoadError = LoadDLLIfNeeded())!=0)
	{
        *ErrorNo = dllLoadError;
		return -1;
	}
	else
	{
		return (*Get_Fifo_Count_Ptr)(FifoNo, DeviceNo,ErrorNo);
	}
}

/* Returns number of free elements in FIFO										  */
/* ============================================================================== */
long Fifo_Empty (short FifoNo, short DeviceNo , long *ErrorNo)
{
    long dllLoadError;
    if ((dllLoadError = LoadDLLIfNeeded())!=0)
	{
        *ErrorNo = dllLoadError;
		return -1;
	}
	else
	{
		return (*Get_Fifo_Empty_Ptr)(FifoNo, DeviceNo,ErrorNo);
	}
}

/* Initiats read and write pointer of FIFO										  */
/* ============================================================================== */
void Fifo_Clear (short FifoNo, short DeviceNo, long *ErrorNo)
{
    long dllLoadError;
    if ((dllLoadError = LoadDLLIfNeeded())!=0)
	{
        *ErrorNo = dllLoadError;
	}
	else
	{
		(*Fifo_Clear_Ptr)(FifoNo, DeviceNo, ErrorNo);
	}
}

/* Returns how busy the CPU is (processor usage in percent)                       */
/* ============================================================================== */
short Workload(long Priority, short DeviceNo, long *ErrorNo)
{
    long dllLoadError;
    if ((dllLoadError = LoadDLLIfNeeded())!=0)
	{
        *ErrorNo = dllLoadError;
		return -1;
	}
	else
	{
		return (*Workload_Ptr)(Priority, DeviceNo, ErrorNo);          /* Ergebnis abholen */
	}
}

/* Checks if the CPU is accessible (previously booted and running)				  */
/* ============================================================================== */
short Test_Version(short DeviceNo, long *ErrorNo)
{
    long dllLoadError;
    if ((dllLoadError = LoadDLLIfNeeded())!=0)
	{
        *ErrorNo = dllLoadError;
		return -1;
	}
	else
	{
		return (*ADTest_Version_Ptr)(DeviceNo, message, ErrorNo);
	}
}

/* Returns value of free memory from the ADwin-2, -4, -5 ,-8, -9, -10 and -11 System(in Bytes) */
/* Returns value of free memory from the ADwin-12 System(in kBytes) */
/* ================================================================================= */
long Free_Mem(short Mem_Spec, short DeviceNo , long *ErrorNo)
{
    long dllLoadError;
    if ((dllLoadError = LoadDLLIfNeeded())!=0)
	{
        *ErrorNo = dllLoadError;
		return -1;
	}
	else
	{
		short Processor = Processor_Type(DeviceNo, ErrorNo);
		switch(Processor)
		{
			case 2:
			case 4:
			case 5:
			case 8:
				switch(Mem_Spec)
				{
					case 0:	/* 0 = T2, T4, T5, T8 */
						return (*AD_Memory_Ptr)(DeviceNo,ErrorNo);							//return value in Byte
					default:
						*ErrorNo = k_mem_type_not_supported;
						return -1;
				}

			/* Mem_Spec */
			/* 1 = PM          : T9, T10, T11 */
			/* 2 = EM          : T11          */
			/* 3 = DM_Local    : T9, T10, T11 */
			/* 4 = DRAM_Extern : T9, T10, T11 */

			/* 5 = Cacheable  : T12   in kBytes */
			/* 6 = Uncacheable: T12   in kBytes */
			case 9:
			case 1010:
				switch(Mem_Spec)
				{
					case 1:
						return (*AD_Memory_all_Ptr)(Mem_Spec, DeviceNo,ErrorNo)*6;	//return value in Byte
					case 3:
					case 4:
						return (*AD_Memory_all_Ptr)(Mem_Spec, DeviceNo,ErrorNo)*4;	//return value in Byte
					default:
						*ErrorNo = k_mem_type_not_supported;
						return -1;
				}
			case 1011:
				switch(Mem_Spec)
				{
					case 1:
					case 2:
					case 3:
					case 4:
						return (*AD_Memory_all_Ptr)(Mem_Spec, DeviceNo,ErrorNo)*4;		//return value in Byte
					default:
						*ErrorNo = k_mem_type_not_supported;
						return -1;
				}
			case 1012:
				switch(Mem_Spec)
				{
					case 5:
					case 6:
						return (*AD_Memory_all_Ptr)(Mem_Spec, DeviceNo,ErrorNo);        //return value in kByte
					default:
						*ErrorNo = k_mem_type_not_supported;
						return -1;
				}
			default:
				*ErrorNo = k_mem_type_not_supported;
				return -1;
		}
	}
}

/* Error messages for Boot, Load_Process    0/1 -> (off/on)        */
/* ============================================================================== */
void Show_Errors(short OnOff)
{
  message=OnOff;
}

/* Gets the error text of the "Error Number"									  */
/* ============================================================================== */
char *Get_Error_Text(long ErrorNo, char *ErrorText , long Length)
{
    if ((short)LoadDLLIfNeeded()!=0)
	{
		
		//strcpy(ErrorText, "dllLoadError in Get_Error_Text");
		strcpy_s(ErrorText, Length, "dllLoadError in Get_Error_Text");
		return (ErrorText);
	}
	else
	{
		(*AD_GetErrorText_Ptr)(ErrorNo, ErrorText, Length);
		return (ErrorText);
	}
}


/* Returns the Processortype												      */
/* ============================================================================== */
short Processor_Type(short DeviceNo,long *ErrorNo)
{
	short merker,Processor_Type;
    long dllLoadError;
    if ((dllLoadError = LoadDLLIfNeeded())!=0)
	{
        *ErrorNo = dllLoadError;
		return -1;
	}
	else
	{
		merker = (*ADProzessorTyp_Ptr)(DeviceNo, ErrorNo);
		Processor_Type = merker;

		if (merker == 146)
		Processor_Type = 5;
		if (merker == 1000)
		Processor_Type = 9;
		// The other processor types are returned direct with the correct number

		return (Processor_Type);
	}
}

/* Clear the Process															  */
/* ============================================================================== */
void Clear_Process(short ProcessNo, short DeviceNo , long *ErrorNo)
{
    long dllLoadError;
    if ((dllLoadError = LoadDLLIfNeeded())!=0)
	{
        *ErrorNo = dllLoadError;
	}
	else
	{
		(*Clear_Process_Ptr)(ProcessNo,DeviceNo,ErrorNo);
	}
}

/* Returns the Processstatus													  */
/* ============================================================================== */
long Process_Status(short ProcessNo, short DeviceNo , long *ErrorNo)
{
	long ProcessStatus;
	short erg;

	erg = (short)(-100 + ProcessNo);
	ProcessStatus = Get_Par(erg,DeviceNo,ErrorNo);

	return (ProcessStatus);
}

/* Gets a block of ADwin long parameters into a long array						  */
/* ============================================================================== */
void Get_Par_Block(long Array[], short Startindex, short Count, short DeviceNo, long *ErrorNo)
{
    long dllLoadError;
    if ((dllLoadError = LoadDLLIfNeeded())!=0)
	{
        *ErrorNo = dllLoadError;
	}
	else
	{
		(*Get_ADBPar_All_Ptr)(Startindex, Count, Array, DeviceNo,ErrorNo);
	}
}

/* Gets a block of ADwin integer parameters into a long array					  */
/* ============================================================================== */
void Get_FPar_Block(float Array[], short Startindex, short Count, short DeviceNo,long *ErrorNo)
{
    long dllLoadError;
    if ((dllLoadError = LoadDLLIfNeeded())!=0)
	{
        *ErrorNo = dllLoadError;
	}
	else
	{
		(*Get_ADBFPar_All_Ptr)(Startindex, Count, Array, DeviceNo,ErrorNo);
	}
}

/* Gets all 80 long parameters (Par_1 - Par_80) into a long array				  */
/* ============================================================================== */
void Get_Par_All(long Array[], short DeviceNo , long *ErrorNo)
{
    long dllLoadError;
    if ((dllLoadError = LoadDLLIfNeeded())!=0)
	{
        *ErrorNo = dllLoadError;
	}
	else
	{
		(*Get_ADBPar_All_Ptr)(1, 80, Array, DeviceNo, ErrorNo);
	}
}

/* Gets all 80 float parameters (Par_1 - Par_80) into a long array				  */
/* ============================================================================== */
void Get_FPar_All(float Array[], short DeviceNo, long *ErrorNo)
{
    long dllLoadError;
    if ((dllLoadError = LoadDLLIfNeeded())!=0)
	{
        *ErrorNo = dllLoadError;
	}
	else
	{
		(*Get_ADBFPar_All_Ptr)(1, 80, Array, DeviceNo,ErrorNo);
	}
}

/* Returns the length of a data													  */
/* ============================================================================== */
long Data_Length(short DataNo, short DeviceNo , long *ErrorNo)
{
    long dllLoadError;
    if ((dllLoadError = LoadDLLIfNeeded())!=0)
	{
        *ErrorNo = dllLoadError;
		return -1;
	}
	else
	{
		long ReturnValue;
		ReturnValue = (*Get_Data_Length_Ptr)(DataNo, DeviceNo, ErrorNo);
		if (ReturnValue == 0)
		{	/*Return value = 0 = Array not declared / Feld ist nicht deklariert  */
			*ErrorNo = k_Data_not_declared;
			return -1;
		}
		else
		{
			return ReturnValue;
		}
	}
}

/* Returns the type (float, integer, short) of a data													  */
/* ============================================================================== */
short Data_Type(short DataNo, char *TypeName, short DeviceNo , long *ErrorNo)
{
	short ret_type;
    long dllLoadError;
    if ((dllLoadError = LoadDLLIfNeeded())!=0)
	{
        *ErrorNo = dllLoadError;
		return -1;
	}
	else
	{
		ret_type = (*Get_Data_Type_Ptr)(DataNo, DeviceNo, ErrorNo);
		if (ret_type == 3) //only return type 3 is not clearly between differnet processors
		{
			//Because ret_type=3 is not realy clear between different processors we check the processor type
			if (Processor_Type(DeviceNo, ErrorNo) == 1012) //if T12 change to type "Long"
			{
				ret_type = 4; //return type is LONG by T12
			}
		}
		//    ret_type =         0   ,   1   ,    2   ,     3    ,   4   ,    5     ,     6   ,    7    , 8 
		//Type_Array[] = {"Undefined", "Byte", "Short", "Int32", "Long", "Float32", "Float64", "Int64", "String"};
		if (TypeName!=NULL) strcpy_s(TypeName, 10, Type_Array[ret_type]);
		//if (TypeName!=NULL) strcpy(TypeName, Type_Array[ret_type]);

		return (ret_type);
	}
}

/* Sets a processdelay for a specified prozess on the ADwin-System				  */
/* ============================================================================== */
void Set_Processdelay(short ProcessNo, long Processdelay, short DeviceNo , long *ErrorNo)
{
    long dllLoadError;
    if ((dllLoadError = LoadDLLIfNeeded())!=0)
	{
        *ErrorNo = dllLoadError;
	}
	else
	{
		Set_Par((short)(-90+ProcessNo), Processdelay , DeviceNo , ErrorNo);
	}

}

/* Gets a processdelay for a specified prozess on the ADwin-System				  */
/* ============================================================================== */
long Get_Processdelay(short ProcessNo, short DeviceNo , long *ErrorNo)
{
    long dllLoadError;
    if ((dllLoadError = LoadDLLIfNeeded())!=0)
	{
        *ErrorNo = dllLoadError;
		return -1;
	}
	else
	{
		return Get_Par((short)(-90+ProcessNo), DeviceNo , ErrorNo );
	}
}


/* Sets the language for the error messages										  */
/* ============================================================================== */
void Set_Language(long language, long *ErrorNo)
{
    long dllLoadError;
    if ((dllLoadError = LoadDLLIfNeeded())!=0)
	{
        *ErrorNo = dllLoadError;
	}
	else
	{
		(*ADSetLanguage_Ptr)(language);
	}
}

/* Returns array-element(s) of char-type from ADwin-System-array (DATA_"DataNo")  */
/* ============================================================================== */
long GetData_String(short DataNo, char *Data, long MaxCount, short DeviceNo , long *ErrorNo)
{
    long dllLoadError;
    if ((dllLoadError = LoadDLLIfNeeded())!=0)
	{
        *ErrorNo = dllLoadError;
		return -1;
	}
	else
	{
		return (*GetData_String_Ptr)(Data, MaxCount+1, DataNo, DeviceNo,ErrorNo);
	}
}

/* Returns the lenght of a string												  */
/* ============================================================================== */
long String_Length(short DataNo, short DeviceNo, long *ErrorNo)
{
    long dllLoadError;
    if ((dllLoadError = LoadDLLIfNeeded())!=0)
	{
        *ErrorNo = dllLoadError;
		return -1;
	}
	else
	{
		return (*String_Length_Ptr)(DataNo, DeviceNo,ErrorNo);
	}
}

/* Write a String into a (DATA_"DataNo")										  */
/* ============================================================================== */
void SetData_String(short DataNo, char *Data, short DeviceNo , long *ErrorNo)
{
    long dllLoadError;
    if ((dllLoadError = LoadDLLIfNeeded())!=0)
	{
        *ErrorNo = dllLoadError;
	}
	else
	{
		(*SetData_String_Ptr)(Data, DataNo, DeviceNo,ErrorNo);
	}
}

/* Returns array-element(s) of short-type from packed ADwin-System-array (DATA_"DataNo") */
/* ============================================================================== */
void GetData_Packed_Short(short DataNo, short *Data, long Startindex, long Count, short DeviceNo , long *ErrorNo)
{
    long dllLoadError;
    if ((dllLoadError = LoadDLLIfNeeded())!=0)
	{
        *ErrorNo = dllLoadError;
	}
	else
	{
		(*GetData_Packed_Ptr)(Data, TYPE_SHORT, DataNo, Startindex, Count, DeviceNo,ErrorNo);
	}
}

/* Returns array-element(s) of long-type from packed ADwin-System-array (DATA_"DataNo") */
/* ============================================================================== */
void GetData_Packed_Long(short DataNo, long *Data, long Startindex, long Count, short DeviceNo , long *ErrorNo)
{
    long dllLoadError;
    if ((dllLoadError = LoadDLLIfNeeded())!=0)
	{
        *ErrorNo = dllLoadError;
	}
	else
	{
		(*GetData_Packed_Ptr)(Data, TYPE_LONG, DataNo, Startindex, Count, DeviceNo,ErrorNo);
	}
}

/* Returns array-element(s) of float-type from packed ADwin-System-array (DATA_"DataNo") */
/* ============================================================================== */
void GetData_Packed_Float(short DataNo, float *Data, long Startindex, long Count, short DeviceNo , long *ErrorNo)
{
    long dllLoadError;
    if ((dllLoadError = LoadDLLIfNeeded())!=0)
	{
        *ErrorNo = dllLoadError;
	}
	else
	{
		(*GetData_Packed_Ptr)(Data, TYPE_FLOAT, DataNo, Startindex, Count, DeviceNo,ErrorNo);
	}
}

/* Returns array-element(s) of double-type from packed ADwin-System-array (DATA_"DataNo") */
/* ============================================================================== */
void GetData_Packed_Double(short DataNo, double *Data, long Startindex, long Count, short DeviceNo , long *ErrorNo)
{
    long dllLoadError;
    if ((dllLoadError = LoadDLLIfNeeded())!=0)
	{
        *ErrorNo = dllLoadError;
	}
	else
	{
		(*GetData_Packed_Ptr)(Data, TYPE_DOUBLE, DataNo, Startindex, Count, DeviceNo,ErrorNo);
	}
}


/* Reads out the known device numbers which are configured in the PC			  */
/* ============================================================================== */
void Get_Known_DeviceNo(short *Devices, long *Count_Devices, long *ErrorNo)
{
	*ErrorNo = 0;
    long dllLoadError;
    if ((dllLoadError = LoadDLLIfNeeded())!=0)
	{
        *ErrorNo = dllLoadError;
	}
	else
	{
		short ret_Fkt = (*Get_Known_DeviceNo_Ptr)(Devices, Count_Devices);
		if (ret_Fkt == 1 && Devices != 0) *ErrorNo = k_count_devices_too_small;
	}
}

/* Reads out the known USB device serialnumbers which are configured in the PC			  */
/* ============================================================================== */
long Get_Known_USB_SerialNo(long *SerialNo, long *ErrorNo)
{
    long dllLoadError;
    if ((dllLoadError = LoadDLLIfNeeded())!=0)
	{
        *ErrorNo = dllLoadError;
	    return -1;
	}
	else
	{
		return (*Get_Known_USB_SerialNo_Ptr)(SerialNo);
	}
}

/* Reads out the known USB device serialnumbers which are configured in the PC			  */
/* ============================================================================== */
void ADwin_Debug_Mode_On(char  *Filename, long Size, long *ErrorNo)
{
    *ErrorNo = 0;
    long dllLoadError;
    if ((dllLoadError = LoadDLLIfNeeded())!=0)
	{
        *ErrorNo = dllLoadError;
	}
	else
	{
		int ReturnValue;
		ReturnValue = (*ADwin_Debug_Mode_On_Ptr)(Filename, Size);
		if (ReturnValue == -1)
		 	*ErrorNo = k_File_name_too_long;
		if (ReturnValue == -2)
			*ErrorNo = k_debug_mode_already_on;
		if (ReturnValue == -3)
			*ErrorNo = k_no_permission_on_registry;
	}
}

/* Reads out the known USB device serialnumbers which are configured in the PC			  */
/* ============================================================================== */
void ADwin_Debug_Mode_Off(long *ErrorNo)
{
    long dllLoadError;
    if ((dllLoadError = LoadDLLIfNeeded())!=0)
	{
        *ErrorNo = dllLoadError;
	}
	else
	{
		(*ADwin_Debug_Mode_Off_Ptr)();
	}
}

/* Returns Fifo-element(s) of short-type from packed ADwin-Fifo-array (DATA_"DataNo") */
/* ============================================================================== */
void GetFifo_Packed_Short(short FifoNo, short Data[], long Count, short DeviceNo , long *ErrorNo)
{
    long dllLoadError;
    if ((dllLoadError = LoadDLLIfNeeded())!=0)
	{
        *ErrorNo = dllLoadError;
	}
	else
	{
		(*Get_Fifo_Ptr)(Data, TYPE_LONG, FifoNo, Count, DeviceNo,ErrorNo);
	}
}

/* The function "GetFifo_Packed_Long " is deleted, because it was not working in the requested way. */
/* ============================================================================== */

/* Reads out the Type of Connection */
/* ============================================================================== */
long Get_Connection_Type(short DeviceNo, long *ErrorNo)
{
    long dllLoadError;
    if ((dllLoadError = LoadDLLIfNeeded())!=0)
	{
        *ErrorNo = dllLoadError;
		return -1;
	}
	else
	{
		return (*Get_Connection_Type_Ptr)(DeviceNo);
		/*Return Value:  -1: ERROR */
		/*                0: PORT (ISA/LINK) */
		/*                1: USB -> Device connected */
		/*                2: TCP/IP */
		/*               11: USB -> Device not connected */
	}
}


/* 05.05.2004 MS
Important:

If an application program reads the MAC for identifying a special ADwin-system and will refuse operation , if the MAC does not match, then the following issue should be considered :

What if an ADwin-system breaks and needs to be replaced ?

Jaeger Messtechnik will not provide a spare part with an IDENTICAL  MAC address or USB- serialnumber !

The application program has to be prepared somehow, that a replacement with an ADwin-system with a DIFFERENT MAC or USB- serialnumber might be necessary !

Firewall settings:
The (*.exe) program, which uses Get_DEV_ID has to be added as an exception in the (XP, Vista, ...) firewall settings.

AND:  It must be ensured, that the (UDP) Port 7000 is not blocked by the firewall.
*/

/* Reads out the USB device serialnumber / the MAC-Adress from specified System   */
/* ============================================================================== */
void Get_Dev_ID(long *value, short DeviceNo, long *ErrorNo)
{
    long dllLoadError;
    if ((dllLoadError = LoadDLLIfNeeded())!=0)
	{
        *ErrorNo = dllLoadError;
	}
	else
	{
		(*Get_Dev_ID_Ptr)(DeviceNo, value,ErrorNo);
	}
}


// End of file


